from google.appengine.ext import db
from google.appengine.ext.db import polymodel


class GameException(Exception):
    code = None
    message = None
    def __init__(self, code=None, message=None):
            self.code=code or -39000
            self.message=message or "Game Error"

    def __str__(self):
        return self.message

class GameDataException(GameException):
    def __init__(self, code=None, message=None):
            self.code=code or -35000
            self.message=message or "GameModel Error"

class GameModelBase(object):
    """
    Abstract Base class for Game App Engine Model objects.
    Implements hiierachical deletion of child objects through the (manually set) _child_collections
    """
    
    _child_collections=()
        
    def __eq__(self, other):
        if issubclass(other.__class__, db.Model):
            return self.key() == other.key()
        else:
            return super(GameModelBase, self) == other

    def __ne__(self, other):
        return not self.__eq__(other)
    
    def full_delete(self):
        for cc in [getattr(self, c) for c in self._child_collections if c in dir(self)]:
            self._delete_children(cc)
        self.delete()
    
    def _delete_children(self, cc):
        for c in list(cc):
            c.full_delete()


class GameModel(GameModelBase, db.Model):
    pass
        
class GamePolyModel(GameModelBase, polymodel.PolyModel):
    pass


